package com.yan.controller;

import java.util.List;

import com.yan.controller.viewModel.login.UserModel;
import com.yan.controller.viewModel.looker.BookInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;;
import com.yan.service.SysAdminService;
import com.yan.vo.Page;

@Controller
@RequestMapping("/sysadmin")
public class SysAdminController {
	@Autowired
	private SysAdminService sysAdminService;

	@RequestMapping("/index")
	public String toIndex() {
		return "/sysadmin/index";
	}


	@RequestMapping("/toAdd")
	public String toAdd() {
		return "sysadmin/add";
	}

	@RequestMapping("/show")
	public String getBooks(Model model, BookInfo book) {
		if (book.getSid() != null) {
			model.addAttribute("sid", book.getSid());
		}
		if (!"".equals(book.getBname()) || book.getBname().length() > 0) {
			model.addAttribute("bname", book.getBname());
		}
		Page page = sysAdminService.getBooks(book);
		model.addAttribute("page", page);
		model.addAttribute("currentPage", book.getCurrentPage());
		model.addAttribute("totalPage", page.getTotalPage());
		return "sysadmin/show";
	}

	@RequestMapping("/add")
	public String addBooks(BookInfo book) {
		sysAdminService.addBooks(book);
		return "redirect:/sysadmin/toAdd";
	}

	@RequestMapping("/selById")
	public String selBookById(Model model, Integer id) {
		BookInfo book = sysAdminService.getBookById(id);
		model.addAttribute("book", book);
		return "sysadmin/update";
	}

	@RequestMapping("/update")
	public String updateBooks(BookInfo book) {
		sysAdminService.updateBooks(book);
		return "redirect:/sysadmin/show";
	}

	@RequestMapping("/delete")
	public String deleteBooks(Integer id) {
		
		sysAdminService.deleteBooks(id);

		return "redirect:/sysadmin/show";
	}

	// 下面部分为user模块
	@RequestMapping("/showUser")
	public String showUser(Model model, UserModel user) {
		List<UserModel> users = sysAdminService.getUsers(user);
		model.addAttribute("users", users);
		return "sysadmin/showUser";
	}

	@RequestMapping("/toAddUser")
	public String toAddUser() {
		return "sysadmin/addUser";
	}

	@RequestMapping("/addUser")
	public String addUser(UserModel user) {
		sysAdminService.addUsers(user);
		return "redirect:/sysadmin/showUser";
	}

	@RequestMapping("/selUserById")
	public String selUserById(Model model, Integer id) {
		UserModel user = sysAdminService.getUserById(id);
		model.addAttribute("user", user);
		return "sysadmin/updateUser";
	}

	@RequestMapping("/updateUser")
	public String updateUser(UserModel user) {
		sysAdminService.updateUser(user);
		return "redirect:/sysadmin/showUser";
	}

	@RequestMapping("/deleteUser")
	public String deleteUser(Integer id,Model model) {
		String errormsg="不允许删除此用户！";
		try {
		sysAdminService.deleteUser(id);
		}catch(Exception e){
			model.addAttribute("errorMsg", errormsg);
		}
		
		return "forward:/sysadmin/showUser";
	}

	@RequestMapping("/checkCode")
	@ResponseBody
	public String checkCode(String code) {
		int user = sysAdminService.selectUsers(code);
		if (user != 0) {
			return "1";
		}
		return "0";
	}

}
